package com.zzyl.service.impl;

import ch.qos.logback.classic.spi.EventArgUtil;
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.zzyl.service.WechatThirdPartyService;
import com.zzyl.vo.GetWechatOpenIdVO;
import com.zzyl.vo.GetWechatPhoneInfoVO;
import com.zzyl.vo.GetWechatTokenVO;
import com.zzyl.vo.WechatPhoneDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class WechatThirdPartyServiceImpl implements WechatThirdPartyService {

    @Resource
    WechatThirdPartyService wechatThirdPartyService;
    @Resource
    private RestTemplate restTemplate;
    @Value("${zzyl.wechat.appId}")
    private String appid;
    @Value("${zzyl.wechat.appSecret}")
    private String secret;

    @Override
    public GetWechatOpenIdVO getOpenId(String code) {
        HashMap<String, String> a = new HashMap<>();
        a.put("appid", appid);
        a.put("secret", secret);
        a.put("js_code", code);
        a.put("grant_type", "authorization_code");
        ResponseEntity<String> forEntity = restTemplate.getForEntity("/sns/jscode2session" +
                        "?appid={appid}&secret={secret}&js_code={js_code}&grant_type={grant_type}",
                String.class, a);
        String body = forEntity.getBody();
        GetWechatOpenIdVO getWechatOpenIdVO = JSONObject.parseObject(body, GetWechatOpenIdVO.class);
//        log.info("请求到了{}",getWechatOpenIdVO);
        return getWechatOpenIdVO;

    }

    @Override
    public GetWechatTokenVO getToken() {
        HashMap<String, String> a = new HashMap<>();
        a.put("appid", appid);
        a.put("secret", secret);
        a.put("grant_type", "client_credential");
        ResponseEntity<String> forEntity = restTemplate.getForEntity("/cgi-bin/token" +
                        "?appid={appid}&secret={secret}&grant_type={grant_type}",
                String.class, a);
        String body = forEntity.getBody();
        GetWechatTokenVO getWechatTokenVO = JSONObject.parseObject(body, GetWechatTokenVO.class);
//        log.info("请求到了{}",getWechatTokenVO);
        return getWechatTokenVO;
    }

    @Override
    public String getPhoneNumber(String phoneCode) {

        //url参数
        HashMap<String, String> map = new HashMap<>();
        map.put("accessToken", getToken().getAccessToken());

        //请求体参数
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", phoneCode);

        ResponseEntity<String> response = restTemplate.postForEntity("/wxa/business/getuserphonenumber?access_token={accessToken}",
                jsonObject.toJSONString(), String.class, map);

        String body = response.getBody();
        String phoneInfo = JSONObject.parseObject(body).getString("phone_info");
        WechatPhoneDetailVO detailVO = JSONObject.parseObject(phoneInfo, WechatPhoneDetailVO.class);

        return detailVO.getPhoneNumber();


//        URI url = URI.create("https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=84_abVoN4vcvb3dQwlXJkcUyVZ5BHgAJh2Gjn_gpYowYQc6QLH_xy7B262seYV2lLs3ALF8rs0vUuaGCUag3UdOyED925PqlWkMm-QRUJC3N0X1H8nFZIcMNttc6FADKTaACANUD");
//
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("code", phoneCode);
//        jsonObject.put("openid", "oP5iu6GdYBNR9JgfoeMp6qZqwcXk");
//        System.out.println("jsonObject = " + jsonObject);
//
//        HttpEntity<?> requestEntity = new HttpEntity<>(jsonObject);
//        ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
//        System.out.println("exchange.getBody() = " + exchange.getBody());
    }
}
